package wpwork.controller;


import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wpwork.base.ActionResult;
import wpwork.base.UserInfo;
import wpwork.base.controller.SuperController;
import wpwork.entity.WorkResourcesEntity;
import wpwork.entity.WorkRsinspectionEntity;
import wpwork.entity.WorkRsinspectionEntity;
import wpwork.service.WorkResourcesService;
import wpwork.service.WorkRsinspectionService;
import wpwork.service.WorkRsinspectionService;
import wpwork.util.ServletUtil;
import wpwork.util.UserProvider;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.UUID;

/**
 * Contract
 * 版本： V3.0.0
 * 版权： 武汉维普科技有限公司(https://www.websharp.cn)
 * 作者： wpwork开发平台组
 * 日期： 2020-12-31
 * 巡检配置
 */
@RestController
@Tag(name = "示例接口", description = "WorkRsinspection")
@RequestMapping("/api/example/WorkRsinspection")
public class WorkRsinspectionController extends SuperController<WorkRsinspectionService, WorkRsinspectionEntity> {

    @Autowired
    private WorkRsinspectionService WorkRsinspectionService;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private WorkResourcesService workResourcesService;
    @Autowired
    private WorkPersonJobController workPersonJobController;


    @PostMapping("/getOne")
    public ActionResult<WorkRsinspectionEntity> getOne(@RequestParam String id) {
        WorkRsinspectionEntity entity = WorkRsinspectionService.getById(id);
        return ActionResult.success(entity);
    }

    @PostMapping("/add")
    public ActionResult<WorkRsinspectionEntity> add(@RequestBody @Valid WorkRsinspectionEntity workRsinspection) {
        WorkRsinspectionEntity entity = new WorkRsinspectionEntity();
        entity.setId(UUID.randomUUID().toString());
        entity.setEnabledMark(workRsinspection.getEnabledMark());
        entity.setMapupload(workRsinspection.getMapupload());
        entity.setInspectionFeedback(workRsinspection.getInspectionFeedback());
        entity.setTerminal(workRsinspection.getTerminal());
        entity.setTime(workRsinspection.getTime());

        entity.setResourcesId(workRsinspection.getResourcesId());
        entity.setCreatorTime(new Date());
        String jwtToken = ServletUtil.getRequest().getHeader("Authorization");
        UserInfo userInfo = userProvider.get(jwtToken);
        entity.setLastModifyUserId(userInfo.getUserId());

        boolean save = WorkRsinspectionService.save(entity);
        WorkResourcesEntity entity2 = new WorkResourcesEntity();
        entity2.setId(workRsinspection.getResourcesId());
        entity2.setInspectionId(entity.getId());
        entity2.setInspection(entity.getEnabledMark());
        workResourcesService.updateById(entity2);
        if(entity.getEnabledMark() == 1
                && !"".equals(entity.getTime()) && !"".equals(entity.getTerminal())
                && entity.getTime() != null && entity.getTerminal() != null
                && !entity.getTime().trim().matches("^\"?\\s*\"?$")
                && !entity.getTerminal().trim().matches("^\"?\\s*\"?$")){
            workPersonJobController.addRsinspectionJob(entity, 0);
        }
        if(save){
            return ActionResult.success("新增成功");
        }else{
            return ActionResult.fail("新增失败");
        }
    }

    @PostMapping("/update")
    public ActionResult<WorkRsinspectionEntity> update(@RequestBody @Valid WorkRsinspectionEntity workRsinspection) {
        WorkRsinspectionEntity entity = new WorkRsinspectionEntity();
        entity.setId(workRsinspection.getId());
        entity.setEnabledMark(workRsinspection.getEnabledMark());
        entity.setMapupload(workRsinspection.getMapupload());
        entity.setInspectionFeedback(workRsinspection.getInspectionFeedback());
        entity.setTerminal(workRsinspection.getTerminal());
        entity.setTime(workRsinspection.getTime());

        entity.setResourcesId(workRsinspection.getResourcesId());
        entity.setLastModifyTime(new Date());
        String jwtToken = ServletUtil.getRequest().getHeader("Authorization");
        UserInfo userInfo = userProvider.get(jwtToken);
        entity.setLastModifyUserId(userInfo.getUserId());
        boolean save = WorkRsinspectionService.updateById(entity);
        WorkResourcesEntity entity2 = new WorkResourcesEntity();
        entity2.setId(workRsinspection.getResourcesId());
        entity2.setInspectionId(entity.getId());
        entity2.setInspection(entity.getEnabledMark());
        workResourcesService.updateById(entity2);
        if(save){
            return ActionResult.success("修改成功");
        }else{
            return ActionResult.fail("修改失败");
        }
    }

}
